<?php

if ( !function_exists('http_curl') ) {
    /**
     * [curl http请求]
     * @param  [type] $url    [description]
     * @param  string $method [description]
     * @param  [type] $data   [description]
     * @return [type]         [description]
     */
    function http_curl($url, $method = 'GET', $data = null)
    {
        $ch = curl_init();
        if ( 'GET' == $method && !empty($data) ) {
            $url .= '?' . http_build_query($data);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        if ( in_array($method,['POST','PUT','DELETE']) ) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}

if ( !function_exists('https_curl') ) {
    /**
     * [curl https请求]
     * @param  [type] $url    [description]
     * @param  string $method [description]
     * @param  [type] $data   [description]
     * @return [type]         [description]
     */
    function https_curl($url, $method = 'GET', $data = null)
    {
        $ch = curl_init();
        if ( 'GET' == $method && !empty($data) ) {
            $url .= '?' . http_build_query($data);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        if ( in_array($method,['POST','PUT','DELETE']) ) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}
